package com.base.core.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入时自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // 方式1: 严格模式填充（推荐）
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        //this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());

        // 方式2: 非严格模式填充
        // this.fillStrategy(metaObject, "createTime", new Date());
         this.fillStrategy(metaObject, "updateTime", new Date());
    }

    /**
     * 更新时自动填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        System.out.println("触发updateFill，当前updateTime: " + metaObject.getValue("updateTime"));
        this.fillStrategy(metaObject, "updateTime", new Date());
        this.setFieldValByName("updateTime", new Date(), metaObject);
        System.out.println("填充后updateTime: " + metaObject.getValue("updateTime"));
    }


}
